﻿
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using RBS.DBUtility;//Please add references
namespace RBS.Core.DAL
{
	/// <summary>
	/// 数据访问类:SYS_User
	/// </summary>
	public partial class SYS_User
	{
		public SYS_User()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string UserName)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from SYS_User");
			strSql.Append(" where UserName=@UserName ");
			SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50)			};
			parameters[0].Value = UserName;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(RBS.Core.Model.SYS_User model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into SYS_User(");
			strSql.Append("UserName,FullName,UserNickName,Password,UserNO,UserType,DeptID,Gender,Birthplace,National,IDCard,Birthday,Email,EmailBacker,OfficePhone,HomePhone,MobilePhone,QQ,MSN,Sequence,AccountState,LastUpdateUserName,LastUpdateUserFullName,LastUpdateTime,CreateUserName,CreateUserFullName,CreateDateTime)");
			strSql.Append(" values (");
			strSql.Append("@UserName,@FullName,@UserNickName,@Password,@UserNO,@UserType,@DeptID,@Gender,@Birthplace,@National,@IDCard,@Birthday,@Email,@EmailBacker,@OfficePhone,@HomePhone,@MobilePhone,@QQ,@MSN,@Sequence,@AccountState,@LastUpdateUserName,@LastUpdateUserFullName,@LastUpdateTime,@CreateUserName,@CreateUserFullName,@CreateDateTime)");
			SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
					new SqlParameter("@FullName", SqlDbType.NVarChar,200),
					new SqlParameter("@UserNickName", SqlDbType.NVarChar,200),
					new SqlParameter("@Password", SqlDbType.VarChar,50),
					new SqlParameter("@UserNO", SqlDbType.VarChar,50),
					new SqlParameter("@UserType", SqlDbType.TinyInt,1),
					new SqlParameter("@DeptID", SqlDbType.VarChar,100),
					new SqlParameter("@Gender", SqlDbType.TinyInt,1),
					new SqlParameter("@Birthplace", SqlDbType.VarChar,50),
					new SqlParameter("@National", SqlDbType.VarChar,100),
					new SqlParameter("@IDCard", SqlDbType.VarChar,18),
					new SqlParameter("@Birthday", SqlDbType.SmallDateTime),
					new SqlParameter("@Email", SqlDbType.VarChar,200),
					new SqlParameter("@EmailBacker", SqlDbType.VarChar,200),
					new SqlParameter("@OfficePhone", SqlDbType.VarChar,500),
					new SqlParameter("@HomePhone", SqlDbType.VarChar,500),
					new SqlParameter("@MobilePhone", SqlDbType.VarChar,100),
					new SqlParameter("@QQ", SqlDbType.VarChar,50),
					new SqlParameter("@MSN", SqlDbType.VarChar,200),
					new SqlParameter("@Sequence", SqlDbType.Int,4),
					new SqlParameter("@AccountState", SqlDbType.TinyInt,1),
					new SqlParameter("@LastUpdateUserName", SqlDbType.VarChar,50),
					new SqlParameter("@LastUpdateUserFullName", SqlDbType.VarChar,50),
					new SqlParameter("@LastUpdateTime", SqlDbType.DateTime),
					new SqlParameter("@CreateUserName", SqlDbType.VarChar,50),
					new SqlParameter("@CreateUserFullName", SqlDbType.VarChar,50),
					new SqlParameter("@CreateDateTime", SqlDbType.DateTime)};
			parameters[0].Value = model.UserName;
			parameters[1].Value = model.FullName;
			parameters[2].Value = model.UserNickName;
			parameters[3].Value = model.Password;
			parameters[4].Value = model.UserNO;
			parameters[5].Value = model.UserType;
			parameters[6].Value = model.DeptID;
			parameters[7].Value = model.Gender;
			parameters[8].Value = model.Birthplace;
			parameters[9].Value = model.National;
			parameters[10].Value = model.IDCard;
			parameters[11].Value = model.Birthday;
			parameters[12].Value = model.Email;
			parameters[13].Value = model.EmailBacker;
			parameters[14].Value = model.OfficePhone;
			parameters[15].Value = model.HomePhone;
			parameters[16].Value = model.MobilePhone;
			parameters[17].Value = model.QQ;
			parameters[18].Value = model.MSN;
			parameters[19].Value = model.Sequence;
			parameters[20].Value = model.AccountState;
			parameters[21].Value = model.LastUpdateUserName;
			parameters[22].Value = model.LastUpdateUserFullName;
			parameters[23].Value = model.LastUpdateTime;
			parameters[24].Value = model.CreateUserName;
			parameters[25].Value = model.CreateUserFullName;
			parameters[26].Value = model.CreateDateTime;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(RBS.Core.Model.SYS_User model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update SYS_User set ");
			strSql.Append("FullName=@FullName,");
			strSql.Append("UserNickName=@UserNickName,");
			strSql.Append("Password=@Password,");
			strSql.Append("UserNO=@UserNO,");
			strSql.Append("UserType=@UserType,");
			strSql.Append("DeptID=@DeptID,");
			strSql.Append("Gender=@Gender,");
			strSql.Append("Birthplace=@Birthplace,");
			strSql.Append("National=@National,");
			strSql.Append("IDCard=@IDCard,");
			strSql.Append("Birthday=@Birthday,");
			strSql.Append("Email=@Email,");
			strSql.Append("EmailBacker=@EmailBacker,");
			strSql.Append("OfficePhone=@OfficePhone,");
			strSql.Append("HomePhone=@HomePhone,");
			strSql.Append("MobilePhone=@MobilePhone,");
			strSql.Append("QQ=@QQ,");
			strSql.Append("MSN=@MSN,");
			strSql.Append("Sequence=@Sequence,");
			strSql.Append("AccountState=@AccountState,");
			strSql.Append("LastUpdateUserName=@LastUpdateUserName,");
			strSql.Append("LastUpdateUserFullName=@LastUpdateUserFullName,");
			strSql.Append("LastUpdateTime=@LastUpdateTime,");
			strSql.Append("CreateUserName=@CreateUserName,");
			strSql.Append("CreateUserFullName=@CreateUserFullName,");
			strSql.Append("CreateDateTime=@CreateDateTime");
			strSql.Append(" where UserName=@UserName ");
			SqlParameter[] parameters = {
					new SqlParameter("@FullName", SqlDbType.NVarChar,200),
					new SqlParameter("@UserNickName", SqlDbType.NVarChar,200),
					new SqlParameter("@Password", SqlDbType.VarChar,50),
					new SqlParameter("@UserNO", SqlDbType.VarChar,50),
					new SqlParameter("@UserType", SqlDbType.TinyInt,1),
					new SqlParameter("@DeptID", SqlDbType.VarChar,100),
					new SqlParameter("@Gender", SqlDbType.TinyInt,1),
					new SqlParameter("@Birthplace", SqlDbType.VarChar,50),
					new SqlParameter("@National", SqlDbType.VarChar,100),
					new SqlParameter("@IDCard", SqlDbType.VarChar,18),
					new SqlParameter("@Birthday", SqlDbType.SmallDateTime),
					new SqlParameter("@Email", SqlDbType.VarChar,200),
					new SqlParameter("@EmailBacker", SqlDbType.VarChar,200),
					new SqlParameter("@OfficePhone", SqlDbType.VarChar,500),
					new SqlParameter("@HomePhone", SqlDbType.VarChar,500),
					new SqlParameter("@MobilePhone", SqlDbType.VarChar,100),
					new SqlParameter("@QQ", SqlDbType.VarChar,50),
					new SqlParameter("@MSN", SqlDbType.VarChar,200),
					new SqlParameter("@Sequence", SqlDbType.Int,4),
					new SqlParameter("@AccountState", SqlDbType.TinyInt,1),
					new SqlParameter("@LastUpdateUserName", SqlDbType.VarChar,50),
					new SqlParameter("@LastUpdateUserFullName", SqlDbType.VarChar,50),
					new SqlParameter("@LastUpdateTime", SqlDbType.DateTime),
					new SqlParameter("@CreateUserName", SqlDbType.VarChar,50),
					new SqlParameter("@CreateUserFullName", SqlDbType.VarChar,50),
					new SqlParameter("@CreateDateTime", SqlDbType.DateTime),
					new SqlParameter("@UserName", SqlDbType.VarChar,50)};
			parameters[0].Value = model.FullName;
			parameters[1].Value = model.UserNickName;
			parameters[2].Value = model.Password;
			parameters[3].Value = model.UserNO;
			parameters[4].Value = model.UserType;
			parameters[5].Value = model.DeptID;
			parameters[6].Value = model.Gender;
			parameters[7].Value = model.Birthplace;
			parameters[8].Value = model.National;
			parameters[9].Value = model.IDCard;
			parameters[10].Value = model.Birthday;
			parameters[11].Value = model.Email;
			parameters[12].Value = model.EmailBacker;
			parameters[13].Value = model.OfficePhone;
			parameters[14].Value = model.HomePhone;
			parameters[15].Value = model.MobilePhone;
			parameters[16].Value = model.QQ;
			parameters[17].Value = model.MSN;
			parameters[18].Value = model.Sequence;
			parameters[19].Value = model.AccountState;
			parameters[20].Value = model.LastUpdateUserName;
			parameters[21].Value = model.LastUpdateUserFullName;
			parameters[22].Value = model.LastUpdateTime;
			parameters[23].Value = model.CreateUserName;
			parameters[24].Value = model.CreateUserFullName;
			parameters[25].Value = model.CreateDateTime;
			parameters[26].Value = model.UserName;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string UserName)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SYS_User ");
			strSql.Append(" where UserName=@UserName ");
			SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50)			};
			parameters[0].Value = UserName;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string UserNamelist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SYS_User ");
			strSql.Append(" where UserName in ("+UserNamelist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public RBS.Core.Model.SYS_User GetModel(string UserName)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 UserName,FullName,UserNickName,Password,UserNO,UserType,DeptID,Gender,Birthplace,National,IDCard,Birthday,Email,EmailBacker,OfficePhone,HomePhone,MobilePhone,QQ,MSN,Sequence,AccountState,LastUpdateUserName,LastUpdateUserFullName,LastUpdateTime,CreateUserName,CreateUserFullName,CreateDateTime from SYS_User ");
			strSql.Append(" where UserName=@UserName ");
			SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.VarChar,50)			};
			parameters[0].Value = UserName;

			RBS.Core.Model.SYS_User model=new RBS.Core.Model.SYS_User();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public RBS.Core.Model.SYS_User DataRowToModel(DataRow row)
		{
			RBS.Core.Model.SYS_User model=new RBS.Core.Model.SYS_User();
			if (row != null)
			{
				if(row["UserName"]!=null)
				{
					model.UserName=row["UserName"].ToString();
				}
				if(row["FullName"]!=null)
				{
					model.FullName=row["FullName"].ToString();
				}
				if(row["UserNickName"]!=null)
				{
					model.UserNickName=row["UserNickName"].ToString();
				}
				if(row["Password"]!=null)
				{
					model.Password=row["Password"].ToString();
				}
				if(row["UserNO"]!=null)
				{
					model.UserNO=row["UserNO"].ToString();
				}
				if(row["UserType"]!=null && row["UserType"].ToString()!="")
				{
					model.UserType=int.Parse(row["UserType"].ToString());
				}
				if(row["DeptID"]!=null)
				{
					model.DeptID=row["DeptID"].ToString();
				}
				if(row["Gender"]!=null && row["Gender"].ToString()!="")
				{
					model.Gender=int.Parse(row["Gender"].ToString());
				}
				if(row["Birthplace"]!=null)
				{
					model.Birthplace=row["Birthplace"].ToString();
				}
				if(row["National"]!=null)
				{
					model.National=row["National"].ToString();
				}
				if(row["IDCard"]!=null)
				{
					model.IDCard=row["IDCard"].ToString();
				}
				if(row["Birthday"]!=null && row["Birthday"].ToString()!="")
				{
					model.Birthday=DateTime.Parse(row["Birthday"].ToString());
				}
				if(row["Email"]!=null)
				{
					model.Email=row["Email"].ToString();
				}
				if(row["EmailBacker"]!=null)
				{
					model.EmailBacker=row["EmailBacker"].ToString();
				}
				if(row["OfficePhone"]!=null)
				{
					model.OfficePhone=row["OfficePhone"].ToString();
				}
				if(row["HomePhone"]!=null)
				{
					model.HomePhone=row["HomePhone"].ToString();
				}
				if(row["MobilePhone"]!=null)
				{
					model.MobilePhone=row["MobilePhone"].ToString();
				}
				if(row["QQ"]!=null)
				{
					model.QQ=row["QQ"].ToString();
				}
				if(row["MSN"]!=null)
				{
					model.MSN=row["MSN"].ToString();
				}
				if(row["Sequence"]!=null && row["Sequence"].ToString()!="")
				{
					model.Sequence=int.Parse(row["Sequence"].ToString());
				}
				if(row["AccountState"]!=null && row["AccountState"].ToString()!="")
				{
					model.AccountState=int.Parse(row["AccountState"].ToString());
				}
				if(row["LastUpdateUserName"]!=null)
				{
					model.LastUpdateUserName=row["LastUpdateUserName"].ToString();
				}
				if(row["LastUpdateUserFullName"]!=null)
				{
					model.LastUpdateUserFullName=row["LastUpdateUserFullName"].ToString();
				}
				if(row["LastUpdateTime"]!=null && row["LastUpdateTime"].ToString()!="")
				{
					model.LastUpdateTime=DateTime.Parse(row["LastUpdateTime"].ToString());
				}
				if(row["CreateUserName"]!=null)
				{
					model.CreateUserName=row["CreateUserName"].ToString();
				}
				if(row["CreateUserFullName"]!=null)
				{
					model.CreateUserFullName=row["CreateUserFullName"].ToString();
				}
				if(row["CreateDateTime"]!=null && row["CreateDateTime"].ToString()!="")
				{
					model.CreateDateTime=DateTime.Parse(row["CreateDateTime"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select UserName,FullName,UserNickName,Password,UserNO,UserType,DeptID,Gender,Birthplace,National,IDCard,Birthday,Email,EmailBacker,OfficePhone,HomePhone,MobilePhone,QQ,MSN,Sequence,AccountState,LastUpdateUserName,LastUpdateUserFullName,LastUpdateTime,CreateUserName,CreateUserFullName,CreateDateTime ");
			strSql.Append(" FROM SYS_User ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" UserName,FullName,UserNickName,Password,UserNO,UserType,DeptID,Gender,Birthplace,National,IDCard,Birthday,Email,EmailBacker,OfficePhone,HomePhone,MobilePhone,QQ,MSN,Sequence,AccountState,LastUpdateUserName,LastUpdateUserFullName,LastUpdateTime,CreateUserName,CreateUserFullName,CreateDateTime ");
			strSql.Append(" FROM SYS_User ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM SYS_User ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.UserName desc");
			}
			strSql.Append(")AS Row, T.*  from SYS_User T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "SYS_User";
			parameters[1].Value = "UserName";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

